package cn.iocoder.yudao.module.trade.controller.admin.order;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.trade.controller.admin.order.vo.*;
import cn.iocoder.yudao.module.trade.service.order.TradeOrderService;
import cn.iocoder.yudao.module.trade.service.order.TradeOrderUpdateService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "管理后台 - 贵金属交易订单")
@RestController
@RequestMapping("/trade/order")
@Validated
public class TradeOrderDealController {
    @Resource
    private TradeOrderService orderService;

    @Resource
    private TradeOrderUpdateService tradeOrderUpdateService;

    @PutMapping("/settle")
    @Operation(summary = "订单结算")
    @PreAuthorize("@ss.hasPermission('trade:order:update')")
    public CommonResult<Boolean> settleOrder(@Valid @RequestBody TradeOrderSettleReqVO settleReqVO) {
        tradeOrderUpdateService.settleOrder(settleReqVO);
        return success(true);
    }
    // 备货完成
    @PutMapping("/prepare-done")
    @Operation(summary = "备货完成")
    @PreAuthorize("@ss.hasPermission('trade:order:update')")
    public CommonResult<Boolean> prepareDone(@RequestParam("orderNo") String orderNo) {
        tradeOrderUpdateService.prepareDone(orderNo);
        return success(true);
    }
    
    @PutMapping("/delivery")
    @Operation(summary = "订单发货")
    @PreAuthorize("@ss.hasPermission('trade:order:update')")
    public CommonResult<Boolean> deliveryOrder(@RequestBody TradeOrderDeliveryReqVO deliveryReqVO) {
        tradeOrderUpdateService.deliveryOrder(deliveryReqVO);
        return success(true);
    }

    @PutMapping("/pick-up-by-verify-code")
    @Operation(summary = "订单核销")
    @Parameter(name = "pickUpVerifyCode", description = "自提核销码")
    @PreAuthorize("@ss.hasPermission('trade:order:pick-up')")
    public CommonResult<Boolean> pickUpOrderByVerifyCode(@RequestParam("pickUpVerifyCode") String pickUpVerifyCode) {
        tradeOrderUpdateService.pickUpOrderByAdmin(getLoginUserId(), pickUpVerifyCode);
        return success(true);
    }
    @PutMapping("/receive")
    @Operation(summary = "订单核销")
    @Parameter(name = "orderNo", description = "自提核销码")
    @PreAuthorize("@ss.hasPermission('trade:order:update')")
    public CommonResult<Boolean> pickUpOrderByAdmin(@RequestParam("orderNo") String orderNo) {
        tradeOrderUpdateService.receiveOrderByAdmin(orderNo);
        return success(true);
    }

    @PutMapping("/update-price")
    @Operation(summary = "订单调价")
    @PreAuthorize("@ss.hasPermission('trade:order:update')")
    public CommonResult<Boolean> updateOrderPrice(@RequestBody TradeOrderUpdatePriceReqVO reqVO) {
        tradeOrderUpdateService.updateOrderPrice(reqVO);
        return success(true);
    }

    @PutMapping("/update-address")
    @Operation(summary = "修改订单收货地址")
    @PreAuthorize("@ss.hasPermission('trade:order:update')")
    public CommonResult<Boolean> updateOrderAddress(@RequestBody TradeOrderUpdateAddressReqVO reqVO) {
        tradeOrderUpdateService.updateOrderAddress(reqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除贵金属交易订单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('trade:order-metals:delete')")
    public CommonResult<Boolean> deleteOrderMetals(@RequestParam("id") String id) {
        orderService.deleteOrderMetals(id);
        return success(true);
    }

    @PutMapping("/update-remark")
    @Operation(summary = "订单备注")
    @PreAuthorize("@ss.hasPermission('trade:order:update')")
    public CommonResult<Boolean> updateOrderRemark(@RequestBody TradeOrderRemarkReqVO reqVO) {
        tradeOrderUpdateService.updateOrderRemark(reqVO);
        return success(true);
    }



    //    @PutMapping("/update")
//    @Operation(summary = "更新贵金属交易订单")
//    @PreAuthorize("@ss.hasPermission('trade:order-metals:update')")
//    public CommonResult<Boolean> updateOrderMetals(@Valid @RequestBody TradeOrderSaveReqVO updateReqVO) {
//        orderService.updateOrderMetals(updateReqVO);
//        return success(true);
//    }



}
